import express from "express";
var router = express.Router();
import path from "path";
// console.debug("======", path.resolve("./ax-assets"));

import { rootPath } from "./api.assets.js";

// //设计模式,预览模式私有静态资源
// router.use(`/axe(d)?/:id`, function (req, res, next) {
//   var params = req.params || {};
//   let url = req.url;
//   console.log("[自定义资源路径处理]", req.url, params);
//   let assetsPath = `${rootPath}/${params.id}`;
//   // if (url.startsWith("/ax-sys/")) {
//   //   assetsPath = `./app-static/axe/ax-sys`; //应用系统资源(公共资源)
//   // }
//   // console.log("[自定义资源代理]", url, " -> ", assetsPath, params);
//   express.static(path.resolve(assetsPath))(req, res, next);
// });

router.use(express.static(path.join(rootPath))); //全平台静态资源
router.use(express.static(path.join("app-static"))); //全平台静态资源

router.use("/axe/:id/", express.static(path.join("app-static/axe")));
router.use("/axed/:id/", express.static(path.join("app-static/axed")));
router.use("/axed/", express.static(path.join("app-static/axed")));

router.use("/axe(d)?/:id/ax-app/", function (req, res, next) {
  console.log("[111]", req.url);
  next();
});

router.use(
  "/axe(d)?/",
  express.static(rootPath, {
    // 设置ETag
    // etag: false,
    // // 设置Last-Modified
    // lastModified: false,
    // 设置缓存控制头，例如'public, max-age=3600'
    setHeaders: (res, path, stat) => {
      console.warn("设置缓存控制头");
      // 自定义设置头部的逻辑
      res.set("Cache-Control", "no-store"); //, must-revalidate, no-store
      // res.set("Pragma", "no-cache");
      // res.set("Expires", "0");
    }
  })
);
router.use("/axe(d)?/:id/", express.static(path.join("app-static")));

// router.use(new RegExp("/.*?/:id"), express.static(path.join("app-static")));
// router.use("(/.*)?/ax-sys", express.static("./", path.join("/app-static/ax-sys"))); //全平台静态资源
//设计模式,预览模式私有静态资源
router.use(new RegExp("/.*?/ax-sys"), function (req, res, next) {
  console.log("[自定义资源路径处理ax-sys]", req.url);
  express.static(path.resolve("app-static/ax-sys"))(req, res, next);
});
// router.use(function (req, res, next) {
//   console.log("[自定义资源路径处理", req.url);
//   // if (req.method != "get") {
//   //   return next();
//   // }

//   if (req.url.startsWith("/ax-apps")) {
//   }

//   try {
//     let url = req.headers.referer || req.headers.host + req.url;
//     let appId = url ? new URL(url).searchParams.get("id") : null;
//     let assetsPath = appId ? `${rootPath}/${appId}` : `${rootPath}`;
//     console.log("[自定义资源代理]", req.url, " -> ", assetsPath);
//     express.static(path.resolve(assetsPath))(req, res, next);
//   } catch (error) {
//     console.error("[自定义资源路径处理异常]", error);
//     next();
//   }
// });

//设计模式api资源管理接口模块
import apiAssets from "./api.assets.js";
router.use(new RegExp(".*?/api/ax.assets"), apiAssets);

// router.use("/axed/:id/", express.static(path.join("./", "/app-static/axed"))); //设计模式可视化应用公共静态资源;
// router.use("/axe(d)?/:id/", express.static(path.join("./", "/app-static/axe"))); //设计模式可视化应用公共静态资源;
// router.use("/axe(d)?/:id/", express.static(path.join("./", "/app-static"))); //设计模式可视化应用公共静态资源;

router.use(express.static(path.join("./", "/app-static"))); //全平台静态资源

//设计模式api资源管理接口模块
import apiAxAppManage from "./api.ax-app-manage.js";
// router.use("/axe(d)?/:id/api/ax-app-manage", apiAxAppManage);
router.use(new RegExp(".*?/api/ax-app-manage"), apiAxAppManage);

router.use(function (req, res, next) {
  console.log("[未处理请求]", req.url);
  next();
});

export default router;
